# -*- coding: utf-8 -*-
"""
Created on Fri Apr 14 21:45:39 2017

@author: tim
"""

from PyQt4 import QtGui,QtCore
import sys
import UiCore

class SearchWin( QtGui.QWidget ):
    def __init__( self ):
        super( SearchWin, self ).__init__()
        self.result = QtGui.QTableWidget( 0, len( UiCore.SEARCH_RESULT_COLUMNS ) )
        self.limiter = {}
        self.setUi()

    def setUi(self):
        self.setWindowTitle( "Search Windows" )
        #set columns of result window 
        self.result.setHorizontalHeaderLabels(UiCore.SEARCH_RESULT_COLUMNS)
        # make the last column setting width autoly to meet the window 
        self.result.horizontalHeader().setStretchLastSection(True)
        self.result.setSelectionBehavior(QtGui.QAbstractItemView.SelectRows|QtGui.QAbstractItemView.SingleSelection)
        self.result.setEditTriggers(QtGui.QAbstractItemView.NoEditTriggers)        
        self.result.setFixedWidth(UiCore.SEARCH_RESULT_WIN_WIDTH)
        h_box = QtGui.QHBoxLayout()
        h_box.addWidget( self.result )
        v_box = QtGui.QVBoxLayout()
        #add search items
        for key in UiCore.SEARCH_KEY:
            newBox = QtGui.QHBoxLayout()
            newLabel = QtGui.QLabel(key)
            newLabel.setAlignment(QtCore.Qt.AlignRight|QtCore.Qt.AlignCenter)
            newLabel.setFixedWidth(UiCore.SEARCH_KEY_WIDTH)
            newBox.addWidget(newLabel)
            newEditLine = QtGui.QLineEdit()
            self.limiter[key] = newEditLine
            newBox.addWidget(newEditLine)
            v_box.addLayout(newBox)
        # add stretch to look well    
        v_box.addStretch(1)
        #Add search button
        searchButton = QtGui.QPushButton( "SEARCH" )
        searchButton.clicked.connect(self.search)
        newBox = QtGui.QHBoxLayout()
        newBox.addStretch(1)        
        newBox.addWidget(searchButton)
        v_box.addLayout(newBox)
        #add add button
        addButton = QtGui.QPushButton("ADD")
        addButton.clicked.connect(self.add)
        newBox = QtGui.QHBoxLayout()
        newBox.addStretch(1)
        newBox.addWidget(addButton)
        v_box.addLayout(newBox)
        # add h box into layout
        h_box.addLayout(v_box)
        self.setLayout(h_box)
        
    def search():
        pass
    
    def add( self, dicts ):
        #clear the table before adding
        for i in range(self.result.rowCount()):
            self.result.removeRow(0)
        for i in range(len(dicts)):
            self.result.insertRow(i)
            for j in range(len(UiCore.SEARCH_RESULT_COLUMNS)):               
                newItem = QtGui.QTableWidgetItem(dicts[i][UiCore.SEARCH_RESULT_COLUMNS[j]])
                self.result.setItem(i, j, newItem)
            




if __name__ == "__main__":
    app = 0
    app = QtGui.QApplication(sys.argv)
    dicts = dict(zip(UiCore.SEARCH_RESULT_COLUMNS,UiCore.SEARCH_RESULT_COLUMNS))
    searchWin = SearchWin()
    searchWin.show()
    searchWin.add([dicts, dicts])
    sys.exit(app.exec_())
        